<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div>

      <el-form-item prop="username">
        <i class="el-icon-user-solid"></i>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <i class="el-icon-attract"></i>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <i :class="passwordType === 'password' ? 'el-icon-thumb' : 'el-icon-view'"></i>
        </span>
      </el-form-item>
      <el-button
        size="medium"
        type="primary"
        :loading="loading"
        :style="'width: 100%; margin-bottom: 30px;border:' + $store.state.$dt_a"
        @click.native.prevent="handleLogin"
        >Login</el-button
      >
    </el-form>
  </div>
</template>

<script>
import { loginToken, getMenuList } from '../../api/table.js'
// import { addDynamicRoutes } from '@/router/permission'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleGetAuthMenu() {
      getMenuList({}).then(res => {
        console.log(res, 'res菜单数据----')
        sessionStorage.setItem('menuList', JSON.stringify(res.data))
        // addDynamicRoutes()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          let data = {
            phone: this.loginForm.username,
            password: this.loginForm.password,
            authType: 1
          }

          loginToken(data).then(res => {
            console.log(res, res.code, res.data.access_token, 'res----')
            if (res.data.access_token) {
              sessionStorage.setItem('userNmae', this.loginForm.username)
              sessionStorage.setItem('token', res.data.access_token)
              this.$router.replace({ path: '/' })
              this.$notify({
                title: '登录成功',
                message: '欢迎回来，【靓仔】',
                type: 'success',
                duration: 3000
              })
            }
            this.handleGetAuthMenu() //获取菜单权限
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
$bg: #545c64;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login .el-input input {
    color: $cursor;
  }
}

/* 重置element-ui css */
.login {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
    .el-form-item__content {
      display: flex;
      align-items: center;
      i {
        margin-left: 15px;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #545c64;
$dark_gray: #889aa4;
$light_gray: #eee;
.login {
  width: 100vw;
  height: 100vh;
  background-color: $bg;
  overflow: hidden;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  i {
    color: $light_gray;
    font-size: 20px;
    line-height: 20px;
  }
}
</style>
